﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System;
using System.IO;
using System.Runtime.InteropServices;

using MyUtils.PInvoke.Structs;

namespace MyUtils.PInvoke
{
	public static class HardLinks
	{
		public static void CreateHardLink(string linkFileName, string existingFileName)
		{
			PInvoke.CreateHardLink(linkFileName, existingFileName);
		}

		public static int GetHardLinksCount(string file)
		{
			using (var stream = File.OpenRead(file))
			{
				ByHandleFileInformation inf;
#pragma warning disable 612,618
				if (PInvoke.GetFileInformationByHandle(stream.Handle, out inf))
#pragma warning restore 612,618
				{
					return (int)inf.NumberOfLinks;
				}
				var err = Marshal.GetLastWin32Error();
				throw new IOException(string.Format(CultureInfo.CurrentCulture, "Can not get file information by handle. Result: {0:X} ({0})", err));
			}
		
		}
	}


}
